const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
    orderSn: null,
    receiverKeyword: null,
    status: null,
    createTime: null,
};

new Vue({
    el: '#app',
    data: function () {
        return {
            visible: false,
            isCollapse: false,
            opened: false,
            isActive: false,
            listQuery: {
                pageNum: 1,
                pageSize: 10,
                orderSn: null,
                receiverKeyword: null,
                status: null,
                createTime: null,
            },
            listLoading: false,
            list: null,
            total: null,
            operateType: null,
            multipleSelection: [],
            closeOrder: {
                dialogVisible: false,
                content: null,
                orderIds: []
            },
            statusOptions: [
                {
                    label: '待付款',
                    value: 0
                },
                {
                    label: '待发货',
                    value: 1
                },
                {
                    label: '已发货',
                    value: 2
                },
                {
                    label: '已完成',
                    value: 3
                },
                {
                    label: '已关闭',
                    value: 4
                }
            ],
            operateOptions: [
                {
                    label: "批量发货",
                    value: 1
                },
                {
                    label: "关闭订单",
                    value: 2
                },
                {
                    label: "删除订单",
                    value: 3
                }
            ],
            logisticsDialogVisible: false
        }
    },
    created() {
        this.getList();
    },
    filters: {
        formatCreateTime(time) {
            let date = new Date(time);
            return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
        },
        formatStatus(value) {
            if (value === 1) {
                return '待发货';
            } else if (value === 2) {
                return '已发货';
            } else if (value === 3) {
                return '已完成';
            } else if (value === 4) {
                return '已关闭';
            } else if (value === 5) {
                return '无效订单';
            } else {
                return '待付款';
            }
        },
    },
    methods: {
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
        sidebarHide() {
            $(".sidebar-container").addClass("sidebar-hide");
            alert("sidebarHide");
        },
        sidebarOpen() {
            $(".sidebar-container").removeClass("sidebar-hide");
            alert("sidebarOpen");
        },
        toggleSideBar() {
            alert("togglesidebar");
        },
        sideBar() {
            alert("sidebar");
        },
        toggleClick() {
            this.isActive = !this.isActive;
            this.isCollapse = !this.isCollapse;
        },
        handleCommand(command) {
            window.location.href = "/home";
        },
        logout() {

        },
        handleResetSearch() {
            this.listQuery = Object.assign({}, defaultListQuery);
        },
        handleSearchList() {
            this.listQuery.pageNum = 1;
            this.getList();
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        handleViewOrder(index, row) {
            window.location.href = "/#";
        },
        handleCloseOrder(index, row) {
            this.closeOrder.dialogVisible = true;
            this.closeOrder.orderIds = [row.id];
        },
        handleDeliveryOrder(index, row) {
            let listItem = this.covertOrder(row);
            window.location.href = "/#";
        },
        handleViewLogistics(index, row) {
            this.logisticsDialogVisible = true;
        },
        handleDeleteOrder(index, row) {
            let ids = [];
            ids.push(row.id);
            this.deleteOrder(ids);
        },
        handleBatchOperate() {
            if (this.multipleSelection == null || this.multipleSelection.length < 1) {
                this.$message({
                    message: '请选择要操作的订单',
                    type: 'warning',
                    duration: 1000
                });
                return;
            }
            if (this.operateType === 1) {
                //批量发货
                let list = [];
                for (let i = 0; i < this.multipleSelection.length; i++) {
                    if (this.multipleSelection[i].status === 1) {
                        list.push(this.covertOrder(this.multipleSelection[i]));
                    }
                }
                if (list.length === 0) {
                    this.$message({
                        message: '选中订单中没有可以发货的订单',
                        type: 'warning',
                        duration: 1000
                    });
                    return;
                }
                window.location.href = "/#";
            } else if (this.operateType === 2) {
                //关闭订单
                this.closeOrder.orderIds = [];
                for (let i = 0; i < this.multipleSelection.length; i++) {
                    this.closeOrder.orderIds.push(this.multipleSelection[i].id);
                }
                this.closeOrder.dialogVisible = true;
            } else if (this.operateType === 3) {
                //删除订单
                let ids = [];
                for (let i = 0; i < this.multipleSelection.length; i++) {
                    ids.push(this.multipleSelection[i].id);
                }
                this.deleteOrder(ids);
            }
        },
        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },
        handleCloseOrderConfirm() {
            if (this.closeOrder.content == null || this.closeOrder.content === '') {
                this.$message({
                    message: '操作备注不能为空',
                    type: 'warning',
                    duration: 1000
                });
                return;
            }
            let params = new URLSearchParams();
            params.append('ids', this.closeOrder.orderIds);
            params.append('note', this.closeOrder.content);
            closeOrder(params).then(response => {
                this.closeOrder.orderIds = [];
                this.closeOrder.dialogVisible = false;
                this.getList();
                this.$message({
                    message: '修改成功',
                    type: 'success',
                    duration: 1000
                });
            });
        },
        getList() {
            // this.listLoading = true;
            // fetchList(this.listQuery).then(response => {
            //     this.listLoading = false;
            //     this.list = response.data.list;
            //     this.total = response.data.total;
            // });
        },
        deleteOrder(ids) {
            this.$confirm('是否要进行该删除操作?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = new URLSearchParams();
                params.append("ids", ids);
                deleteOrder(params).then(response => {
                    this.$message({
                        message: '删除成功！',
                        type: 'success',
                        duration: 1000
                    });
                    this.getList();
                });
            })
        },
        covertOrder(order) {
            let address = order.receiverProvince + order.receiverCity + order.receiverRegion + order.receiverDetailAddress;
            let listItem = {
                orderId: order.id,
                orderSn: order.orderSn,
                receiverName: order.receiverName,
                receiverPhone: order.receiverPhone,
                receiverPostCode: order.receiverPostCode,
                address: address,
                deliveryCompany: null,
                deliverySn: null
            };
            return listItem;
        }
    }
})
function formatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    };
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k] + '';
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
      }
    }
    return fmt;
  }
  function fetchList(params) {
    return request({
      url:'/#',
      method:'get',
      params:params
    })
  }
  
function closeOrder(params) {
    return request({
      url:'/#',
      method:'post',
      params:params
    })
  }
  
function deleteOrder(params) {
    return request({
      url:'/#',
      method:'post',
      params:params
    })
  }